﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.DAO;
using DHQGHN.DAL.DAO.MappingDB;
using DHQGHN.DAL.DataMapper;
using DHQGHN.DAL.Models;
using DHQGHN.DAL.Models.MappingDB;

namespace DHQGHN.DAL.BO.MappingDB
{
    public class SD_MappingTableBO : BaseBO
    {
        #region Field Name in return DataSet
        //Field Name in return DataSet
        public const string Field_MappingTableId = "MappingTableId";
        public const string Field_ConfigSourceID = "ConfigSourceID";
        public const string Field_ConfigDestinationID = "ConfigDestinationID";
        public const string Field_SourceTable = "SourceTable";
        public const string Field_DestinationTable = "DestinationTable";
        public const string Field_SourceTableSchema = "SourceTableSchema";
        
        #endregion


        public String _connectionString;
        private SD_MappingTableDAO _shareDAO;
        public SD_MappingTableBO(string connString)
        {
            this._connectionString = connString;

        }
        public SD_MappingTableBO()
        {
            this._connectionString = DEFAULT_CONN_STRING;

        }

        public SD_MappingTableDAO CreateSQLDAO()
        {
            return new SD_MappingTableDAO(_connectionString, DEFAULT_MAPPER_TYPE);
        }

        public SD_MappingTable GetMappingTableById(int mappingTableId)
        {
            try
            {
                _shareDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    
                    {SD_MappingTable.Field_MappingTableId,Field_MappingTableId},
                    {SD_MappingTable.Field_DestinationTable,Field_DestinationTable},
                    {SD_MappingTable.Field_SourceTable,Field_SourceTable},
                    {SD_MappingTable.Field_ConfigSourceID,Field_ConfigSourceID},
                    {SD_MappingTable.Field_ConfigDestinationID,Field_ConfigDestinationID},                
                                   
                    
                };
                return _shareDAO.GetMappingTableById(mappingTableId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public SD_MappingTable GetMappingTableBySourceTable(int sourceId, string tableName)
        {
            try
            {
                _shareDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {SD_MappingTable.Field_MappingTableId,Field_MappingTableId},
                    {SD_MappingTable.Field_ConfigSourceID,Field_ConfigSourceID},     
                    {SD_MappingTable.Field_ConfigDestinationID,Field_ConfigDestinationID},                
                    {SD_MappingTable.Field_SourceTable,Field_SourceTable},                
                    {SD_MappingTable.Field_DestinationTable,Field_DestinationTable},
                };
                return _shareDAO.GetMappingTableBySourceTable(sourceId,tableName, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

      


        public SD_MappingTable Save(SD_MappingTable model)
        {
            try
            {
                _shareDAO = CreateSQLDAO();
                return _shareDAO.Save(model);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int DeleteSourceTable(SD_MappingTable model)
        {
            try
            {
                _shareDAO = CreateSQLDAO();
                return _shareDAO.Delete(model);
            }
            catch (Exception)
            {
                throw;
            }
        }

       

        public List<SD_MappingTable> GetAllTableNameFromMetaData(int sourceId)
        {
            try
            {
                _shareDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {SD_MappingTable.Field_ConfigSourceID,Field_ConfigSourceID},
                    {SD_MappingTable.Field_SourceTable,Field_SourceTable},
                    {SD_MappingTable.Field_DestinationTable,Field_DestinationTable},
                    {SD_MappingTable.Field_SourceTableSchema,Field_SourceTableSchema}
                };
                return _shareDAO.GetAllTableNameFromMetaData(sourceId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<SD_MappingTable> GetAllTableNameFromMapDB(int sourceId)
        {
            try
            {
                _shareDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {SD_MappingTable.Field_ConfigSourceID,Field_ConfigSourceID},
                    {SD_MappingTable.Field_SourceTable,Field_SourceTable},
                    {SD_MappingTable.Field_MappingTableId,Field_MappingTableId},
                    {SD_MappingTable.Field_ConfigDestinationID,Field_ConfigDestinationID},  
                    {SD_MappingTable.Field_DestinationTable,Field_DestinationTable},  
                    
                };
                return _shareDAO.GetAllTableNameFromMapDB(sourceId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
